package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;

/**
 * Created by yanggeng on 2017/7/6.
 */

public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar toolbar;
    protected View main_view;
    private LinearLayout main_content, errorLayout;
    protected ImageView iv_cancel, iv_right;
    protected TextView tv_left, tv_title, tv_right;

    public static final int LEFT_BUTTON = 1;
    public static final int RIGHT_BUTTON = 2;
    public static final int LEFT_TEXT = 3;
    public static final int RIGHT_TEXT = 4;
    protected Context context;
    protected String TAG;

    protected Toolbar getToolbar() {
        return toolbar;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        context = this;
        main_view = findViewById(R.id.main_view);
        iv_cancel = (ImageView) findViewById(R.id.iv_cancel);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        tv_left = (TextView) findViewById(R.id.tv_left);
        tv_right = (TextView) findViewById(R.id.tv_right);
        iv_right.setOnClickListener(this);
        iv_cancel.setOnClickListener(this);
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        TAG = getLocalClassName();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
        setStatusBarUpperAPI21();
    }

    @Override
    public void setContentView(View view) {
        main_content = (LinearLayout) findViewById(R.id.main_content);
        errorLayout = (LinearLayout) findViewById(R.id.ll_load_fail);
        if(main_content == null){
            return;
        }
        main_content.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        initToolbar();
     }

    private void initToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        int paddingTop = toolbar.getPaddingTop();
        int paddingBottom = toolbar.getPaddingBottom();
        int paddingLeft = toolbar.getPaddingLeft();
        int paddingRight = toolbar.getPaddingRight();
        int statusHeight = getStatusHeight();
        int toolbarHeight = toolbar.getLayoutParams().height;

//        paddingTop += statusHeight;
//        toolbar.getLayoutParams().height = toolbarHeight + statusHeight;

        toolbar.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        setSupportActionBar(toolbar);
    }

    protected void hideToolbar() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        toolbar.setVisibility(View.GONE);
    }

    protected void setTitle(String title) {
        setTitle(false, title);
    }

    protected void setTitle(boolean showCancel, String title) {
        setTitle(showCancel, title, -1);
    }

    protected void setTitle(boolean showCancel, String title, int rightResId) {
        if(showCancel) {
            iv_cancel.setVisibility(View.VISIBLE);
        }
        tv_title.setText(title);
        if(rightResId != -1) {
            iv_right.setImageResource(rightResId);
            iv_right.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitle(boolean showCancel, String title, String rightText) {
        if(showCancel) {
            iv_cancel.setVisibility(View.VISIBLE);
        }
        tv_title.setText(title);
        if(!TextUtils.isEmpty(rightText)) {
            tv_right.setText(rightText);
            tv_right.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitle(String leftText, String title, int rightResId) {
        if(!TextUtils.isEmpty(leftText)) {
            tv_left.setText(leftText);
            tv_left.setVisibility(View.VISIBLE);
        }
        tv_title.setText(title);
        if(rightResId != -1) {
            iv_right.setImageResource(rightResId);
            iv_right.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitle(String leftText, String title) {
        setTitle(leftText, title, "");
    }

    protected void setTitle(String leftText, String title, String rightText) {
        if(!TextUtils.isEmpty(leftText)) {
            tv_left.setText(leftText);
            tv_left.setVisibility(View.VISIBLE);
        }
        tv_title.setText(title);
        if(!TextUtils.isEmpty(rightText)) {
            tv_right.setText(rightText);
            tv_right.setVisibility(View.VISIBLE);
        }
    }

    private int getStatusHeight() {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
           case R.id.iv_cancel:
                navigationOnclick(LEFT_BUTTON);
                finish();
                break;
            case R.id.iv_right:
                navigationOnclick(RIGHT_BUTTON);
                break;
            case R.id.tv_left:
                navigationOnclick(LEFT_TEXT);
                finish();
                break;
            case R.id.tv_right:
                navigationOnclick(RIGHT_TEXT);
                break;
        }
    }

    private void setStatusBarUpperAPI21() {
        Window window = getWindow();
        //设置透明状态栏,这样才能让 ContentView 向上
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
            ViewCompat.setFitsSystemWindows(mChildView, false);
        }
    }

    protected void navigationOnclick(int paramId) {
    }

    protected void showErrorLayout() {
        if (errorLayout.isShown()) {
            return;
        }
        errorLayout.setVisibility(View.VISIBLE);
        main_content.getChildAt(2).setVisibility(View.GONE);
    }

    protected void showContent() {
        if (errorLayout.isShown()) {
            errorLayout.setVisibility(View.GONE);
            main_content.getChildAt(2).setVisibility(View.VISIBLE);
        }
    }
}
